/**
 * 
 */
package co.gov.medellin.sdep.sisdep.operativa.service;

import java.util.Date;
import java.util.List;

import co.gov.medellin.sdep.sisdep.operativa.exception.CitacionException;
import co.gov.medellin.sdep.sisdep.operativa.exception.CitacionRuntimeException;
import co.gov.medellin.sdep.sisdep.operativa.exception.ComparendoException;
import co.gov.medellin.sdep.sisdep.operativa.exception.ComparendoRuntimeException;
import co.gov.medellin.sdep.sisdep.operativa.exception.descriptor.CitacionMessageDescriptor;
import co.gov.medellin.sdep.sisdep.operativa.exception.descriptor.ComparendoMessageDescriptor;
import co.gov.medellin.sdep.sisdep.common.message.MessageDescriptorConstant;
import co.gov.medellin.sdep.sisdep.common.service.BaseService;
import co.gov.medellin.sdep.sisdep.model.Citacion;
import co.gov.medellin.sdep.sisdep.model.Comparendo;
import co.gov.medellin.sdep.sisdep.model.dao.impl.IComparendoDAOT;

/**
 * @author Usuario
 *
 */
public class ComparendoServiceImpl extends BaseService implements IComparendoService {

	//----------------------------------------------------------------------
	//--- propiedades y constructores                                 init()
	//----------------------------------------------------------------------

	/**
	 * default serial version
	 */
	private static final long serialVersionUID = 1L;
	/** */
	private IComparendoDAOT comparendoDAO;

	/**
	 * 
	 */
	public ComparendoServiceImpl() {
	}

	//----------------------------------------------------------------------
	//--- propiedades y constructores                                  end()
	//----------------------------------------------------------------------

	//----------------------------------------------------------------------
	//--- implementacion de metodos para acciones                     init()
	//----------------------------------------------------------------------

	/**
	 * 
	 */
	public void insert(Comparendo comparendo) throws ComparendoException {
		try{
			comparendo.setUsuarioCrea(getPrincipalUser());
			comparendo.setFechaCrea(new Date());
			getComparendoDAO().insert(comparendo);
		}catch (Exception e) {
			Object [] infoError = {
				comparendo.getCodigo()
			};
			if(e.getCause().toString().indexOf("UNIQUE KEY") >= 0){
				throw new ComparendoException(new ComparendoMessageDescriptor(MessageDescriptorConstant.UK_VIOLATION), infoError, e);
			}else{
				throw new ComparendoException(new ComparendoMessageDescriptor(MessageDescriptorConstant.SAVE), infoError, e);
			}
		}
	}

	/**
	 * 
	 */
	public final void update(Comparendo comparendo) throws ComparendoException {
		try{
			comparendo.setUsuarioModifica(getPrincipalUser());
			
			getComparendoDAO().update(comparendo);
		}catch (Exception e) {
			Object [] infoError = {
				comparendo.getCodigo()
			};
			if(e.getCause().toString().indexOf("UNIQUE KEY") >= 0){
				throw new ComparendoRuntimeException(new ComparendoMessageDescriptor(MessageDescriptorConstant.UK_VIOLATION), infoError, e);
			}else{
				throw new ComparendoRuntimeException(new ComparendoMessageDescriptor(MessageDescriptorConstant.UPDATE), infoError, e);
			}
		}
	}

	/**
	 * 
	 */
	public final void delete(Comparendo comparendo) throws ComparendoException {
		try {
			comparendo.setUsuarioModifica(getPrincipalUser());
			comparendo.setFechaModifica(new Date());
			getComparendoDAO().delete(comparendo);
		}catch (Exception e) {
			Object [] infoError = {
				comparendo.getCodigo()
			};
			throw new ComparendoRuntimeException(new ComparendoMessageDescriptor(MessageDescriptorConstant.DELETE), infoError, e);
		}
	}

	/**
	 * @return 
	 * 
	 */
	public final List load(int start,int max, Comparendo comparendo) throws ComparendoException {
		try{
			//return getComparendoDAO().getAllComparendoPaginated(start, max, criteria);
			return getComparendoDAO().load();
		}catch (Exception e) {
			Object [] infoError = null;
			throw new ComparendoRuntimeException(new ComparendoMessageDescriptor(MessageDescriptorConstant.RETRIEVE), infoError, e);
		}
	}
	
	/**
	 * @return 
	 * 
	 */
	public final List loadByRadicado(int start,int max, Comparendo comparendo) throws ComparendoException {
		try{
			//return getComparendoDAO().getAllComparendoPaginated(start, max, criteria);
			return getComparendoDAO().loadByRadicado(comparendo);
		}catch (Exception e) {
			Object [] infoError = null;
			throw new ComparendoRuntimeException(new ComparendoMessageDescriptor(MessageDescriptorConstant.RETRIEVE), infoError, e);
		}
	}
	
	/**
	 * @return 
	 * 
	 */
	public final List listaInfracciones() throws ComparendoException {
		try{
			//return getComparendoDAO().getAllComparendoPaginated(start, max, criteria);
			return getComparendoDAO().listaInfracciones();
		}catch (Exception e) {
			Object [] infoError = null;
			throw new ComparendoRuntimeException(new ComparendoMessageDescriptor(MessageDescriptorConstant.RETRIEVE), infoError, e);
		}
	}

	/**
	 * 
	 */
	public final Comparendo get(long id) throws ComparendoException {
		try{
			return getComparendoDAO().findByPrimaryKey(id);
		}catch (Exception e) {
			Object [] infoError = {new Long(id)};
			throw new ComparendoRuntimeException(new ComparendoMessageDescriptor(MessageDescriptorConstant.GET), infoError, e);
		}
	}
	
	/**
	 * @return 
	 * 
	 */
	public final String verificarDocumento(Comparendo comparendo) throws ComparendoException {
		try{
			//return getCitacionDAO().getAllCitacionPaginated(start, max, criteria);
			return getComparendoDAO().verificarDocumento(comparendo);
		}catch (Exception e) {
			Object [] infoError = null;
			throw new CitacionRuntimeException(new CitacionMessageDescriptor(MessageDescriptorConstant.RETRIEVE), infoError, e);
		}
	}

	//----------------------------------------------------------------------
	//--- implementacion de metodos para acciones                      end()
	//----------------------------------------------------------------------

	//----------------------------------------------------------------------
	//--- getters and setters                                         init()
	//----------------------------------------------------------------------

	/**
	 * @author Usuario
	 *
	 */
	public IComparendoDAOT getComparendoDAO() {
		return comparendoDAO;
	}

	/**
	 * @author Usuario
	 *
	 */
	public void setComparendoDAO(IComparendoDAOT comparendoDAO) {
		this.comparendoDAO = comparendoDAO;
	}

	//----------------------------------------------------------------------
	//--- getters and setters                                          end()
	//----------------------------------------------------------------------

}
